<?php


namespace app\api\controller;


use app\admin\model\score\order\Log;
use app\common\controller\Api;
use app\common\model\CommsionLog;
use app\common\model\GoodsSales;
use app\common\model\LoopConsumption;
use app\common\model\PlatfromComssion;
use app\common\model\PlatfromComssionLog;
use app\common\model\ShopComssion;
use app\common\model\user\Appoints;
use app\common\model\UserCards;
use think\Db;
use think\Model;

class Crontab  extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 30分钟-自动取消订单
     */
    public function orderCancel()
    {
        $time  =time();
//        $mtime   = strtotime(date('Y-m-d',time()));
//        $mtime1  =strtotime(date('Y-m-d',strtotime('-1 days')));
        $order =\app\common\model\order\Order::with(['orderitem'])
            ->where(['status'=>0])
            ->where('pre_time','<>',0)
           ->where('pre_time','<',$time)
            ->select();


        foreach ($order as $k=>$v){
            $order= \db('order')->where(['id'=>$v['id'],'status'=>0])->find();
            if($order){
                \db('order')->where(['id'=>$v['id']])->update(['order_status'=>-1,'status'=>-1]);
                foreach ($v['orderitem'] as $k1=>$v1){
                    $exist = \db('goods')->where(['id'=>$v1['goods_id']])->find();
                    if($exist){
                        $ctime=strtotime(date('Y-m-d',$v1['createtime']));
                        db('goods')->where('id', $v1['goods_id'])->setDec('goods_sales', $v1['goods_num']);
                        db('goods')->where('id', $v1['goods_id'])->setInc('goods_num', $v1['goods_num']);

                    }

                }
            }




        }

    }

    public function finish()
    {
        $time  =date('Y-m-d');
        $time1=time();
//        $mtime   = strtotime(date('Y-m-d',time()));
//        $mtime1  =strtotime(date('Y-m-d',strtotime('-1 days')));
      \app\common\model\order\Order::where(['status'=>1])
            ->where('order_date','<=',$time1)
            ->update(['status'=>2,'order_status'=>2]);
        UserCards::where(['status'=>1])
            ->where('rights_endtime','<=',$time1)
            ->update(['status'=>4]);


    }

    public function pre_finish()
    {

        $time=time();
//        $mtime   = strtotime(date('Y-m-d',time()));
//        $mtime1  =strtotime(date('Y-m-d',strtotime('-1 days')));
        Appoints::where(['status'=>0])
            ->where('appoint_endtime','<=',$time)
            ->update(['status'=>-1]);



    }

}